package com.mgvlife.ddlw.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.mgvlife.ddlw.formbean.AdminForm;
import com.mgvlife.ddlw.service.AdminService;
import com.mgvlife.ddlw.util.HttpSessionUtil;
import com.mgvlife.ddlw.vo.Result;
import com.mgvlife.ddlw.vo.VoAdmin;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.xinhuo.util.JsonUtil;
import com.xinhuo.vo.JsonEasyUI;

@Controller("adminAction")
@Scope("prototype")
public class AdminAction extends ActionSupport implements
		ModelDriven<AdminForm> {

	private static final long serialVersionUID = 7862406371446431713L;
	private AdminForm dataForm = new AdminForm();
	@Resource
	private AdminService adminService = null;

	public void login() {
		Result result = null;
		try {
			result = adminService.adminLogin(dataForm);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JsonUtil.output(result);
		}
	}

	public String logOut() {
		HttpSessionUtil.removeAdmin();
		return "logout";
	}

	public void add() {
		Result result = null;
		try {
			result = adminService.adminAdd(dataForm);
		} catch (Exception e) {
			e.printStackTrace();
			result = Result.unknownError();
		} finally {
			JsonUtil.output(result);
		}
	}

	public void update() {
		Result result = null;
		try {
			result = adminService.adminUpdate(dataForm);
		} catch (Exception e) {
			e.printStackTrace();
			result = Result.unknownError();
		} finally {
			JsonUtil.output(result);
		}
	}

	public void list() {
		JsonEasyUI<VoAdmin> result = null;
		try {
			result = adminService.getData(dataForm);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JsonUtil.output(result);
		}
	}

	public void delete() {
		Result result = null;
		try {
			result = adminService.adminDelete(dataForm);
		} catch (Exception e) {
			e.printStackTrace();
			result = Result.unknownError();
		} finally {
			JsonUtil.output(result);
		}
	}

	@Override
	public AdminForm getModel() {
		return dataForm;
	}

}
